var express = require('express');
var router = express.Router();
var {encode, decode} =require('node-base64-image')
var Cloud = require('leancloud-storage')

/* GET home page. */
router.get('/', function(req, res, next) {
	res.render('index', {
		title: 'Express'
	});
});

router.post('/exchange', async function(req, res, next) {
	let url = req.body.img || 'https://static.mcake.com/new_goods/zhishixiaosheng/R0230/list/1.jpg'
	// console.log(url);
	const options = {
	  string: true,
	  headers: {
	    // 'if-none-match': "8A01E4C72AC27BC62FFC08294AD12204"
	    // 'sec-fetch-dest': 'document'
	    // 'sec-fetch-mode': 'navigate'
	    // 'sec-fetch-site': 'none'
	    // 'sec-fetch-user': '?1'
	    // 'upgrade-insecure-requests': '1'
	    'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1 Edg/99.0.4844.51'
	  }
	};
	
	// writing to file named 'example.jpg'
	const image = await encode(url, options);
	// console.log(image);
	let base64 = 'data:image/jpg;base64,'+image
	const file = new Cloud.File('goods.png', {base64});
	file.save().then(source=>{
		console.log(source);
		let {url} = source.attributes
		res.send({
			code:1,
			msg:'转存图片成功',
			data:url
		})
	})
});

module.exports = router;
